<?php
/**
 * Created by 橙橙同学.
 * User: 橙橙同学
 * Date: 2017/11/19
 * Time: 下午3:17
 */

namespace app;


class Oauth
{

    public static function checkFansAuth()
    {
        global $_W;
        $fan = mc_fansinfo($_W['openid'], $_W['acid'], $_W['uniacid']);
        if (empty($fan) && $_W['account']['level'] == ACCOUNT_SERVICE_VERIFY) {
            $fan = mc_oauth_userinfo();
            if (!empty($fan['openid'])) {
                $fan = mc_fansinfo($fan['openid']);
            }else{
                $fan = '';
            }
        }
        return $fan;
    }



    public static function checkauth() {
        global $_W, $engine;
        load()->model('mc');
        if(!empty($_W['member']) && (!empty($_W['member']['mobile']) || !empty($_W['member']['email']))) {
            return true;
        }
        if(!empty($_W['openid'])) {
            $fan = mc_fansinfo($_W['openid'], $_W['acid'], $_W['uniacid']);
            if (empty($fan) && $_W['account']['level'] == ACCOUNT_SERVICE_VERIFY) {
                $fan = mc_oauth_userinfo();
                if (!empty($fan['openid'])) {
                    $fan = mc_fansinfo($fan['openid']);
                }
            }
            if(_mc_login(array('uid' => intval($fan['uid'])))) {
                return true;
            }
            if (defined('IN_API')) {
                $GLOBALS['engine']->died("抱歉，您需要先登录才能使用此功能，点击此处 <a href='".__buildSiteUrl(url('auth/login')) ."'>【登录】</a>");
            }
        }
        $forward = base64_encode($_SERVER['QUERY_STRING']);

        var_dump($forward);


        /*
        if($_W['isajax']) {
            $result = array();
            $result['url'] = url('auth/login', array('forward' => $forward), true);
            $result['act'] = 'redirect';
            exit(json_encode($result));
        } else {
            header("location: " . url('auth/login', array('forward' => $forward)), true);
        }
        */
        exit;
    }
}